package com.yxq.mdt.app.modules.goodsdetail.ui;


import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.modules.goodsdetail.bean.GoodsCommentBean;
import com.yxq.mdt.app.modules.goodsdetail.bean.GoodsDetailBean;
import com.yxq.mdt.app.modules.goodsdetail.model.GoodsModel;
import com.yxq.mdt.app.modules.goodsdetail.presenter.GoodsPresenter;
import com.yxq.mdt.app.modules.goodsdetail.presenter.IGoodsContract;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 商品详情-评论
 */
public class GoodsDetailCommentFragment extends BaseFragment<GoodsPresenter> implements IGoodsContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    int page = 1;
    @BindView(R.id.goods_detail_comment_tv_all)
    TextView goodsDetailCommentTvAll;
    @BindView(R.id.goods_detail_comment_tv_good)
    TextView goodsDetailCommentTvGood;
    @BindView(R.id.goods_detail_comment_tv_middle)
    TextView goodsDetailCommentTvMiddle;
    @BindView(R.id.goods_detail_comment_tv_bad)
    TextView goodsDetailCommentTvBad;
    @BindView(R.id.goods_detail_comment_tv_pic)
    TextView goodsDetailCommentTvPic;
    GoodsCommentListAdapter goodsCommentListAdapter;
    List<GoodsCommentBean.ListBean> listBeanList;

    @Override
    protected int attachLayoutId() {
        setImmersionBarEnabled(false);
        return R.layout.fragment_goods_detail_recommend;
    }

    @Override
    protected GoodsPresenter getPresenter() {
        return new GoodsPresenter(this, new GoodsModel());
    }

    @Override
    public void initView(View view) {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getCommentList(getArguments().getString("id"), page);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getCommentList(getArguments().getString("id"), page);
        });
    }

    @Override
    public void initData() {
        listBeanList = new ArrayList<>();
        goodsCommentListAdapter = new GoodsCommentListAdapter(R.layout.layout_goods_detail_comment_item_layout, listBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(goodsCommentListAdapter);
        goodsCommentListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        goodsCommentListAdapter.isUseEmpty(false);
        mPresenter.getCommentList(getArguments().getString("id"), 1);
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onLoadSuccess(GoodsDetailBean goodsDetailBean) {

    }

    @Override
    public void onLoadFailed(String err) {

    }

    @Override
    public void onLoadRecommendSuccess(MallGoodsBean mallGoodsBean) {

    }

    @Override
    public void onLoadRecommendFailed(String err) {

    }

    @Override
    public void onAddSuccess(String err) {

    }

    @Override
    public void onAddFailed(String err) {

    }

    @Override
    public void onRemoveSuccess(String err) {

    }

    @Override
    public void onRemoveFailed(String err) {

    }

    @Override
    public void onLoadCommentSuccess(GoodsCommentBean goodsCommentBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (goodsCommentBean == null) {
            return;
        }
        if (page == 1) {
            bindUI(goodsCommentBean);
            goodsCommentListAdapter.setNewData(goodsCommentBean.getList());
        } else {
            if (goodsCommentBean.getList() != null && goodsCommentBean.getList().size() != 0) {
                goodsCommentListAdapter.addData(goodsCommentBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        goodsCommentListAdapter.isUseEmpty(!(goodsCommentListAdapter.getData().size() > 0));
    }

    /**
     * 绑定UI数据
     *
     * @param goodsCommentBean
     */
    private void bindUI(GoodsCommentBean goodsCommentBean) {
        if (goodsCommentBean == null) {
            return;
        }
        goodsDetailCommentTvGood.setText(String.format("好评(%s)", goodsCommentBean.getComment_count().getScore_3()));
        goodsDetailCommentTvMiddle.setText(String.format("中评(%s)", goodsCommentBean.getComment_count().getScore_2()));
        goodsDetailCommentTvBad.setText(String.format("差评(%s)", goodsCommentBean.getComment_count().getScore_1()));
    }

    @Override
    public void onLoadCommentFailed(String err) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onAddCartSuccess(String result) {

    }

    @Override
    public void onAddCartFailed(String err) {

    }

    @OnClick({R.id.goods_detail_comment_tv_all, R.id.goods_detail_comment_tv_good, R.id.goods_detail_comment_tv_middle, R.id.goods_detail_comment_tv_bad, R.id.goods_detail_comment_tv_pic})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.goods_detail_comment_tv_all:
                break;
            case R.id.goods_detail_comment_tv_good:
                break;
            case R.id.goods_detail_comment_tv_middle:
                break;
            case R.id.goods_detail_comment_tv_bad:
                break;
            case R.id.goods_detail_comment_tv_pic:
                break;
        }
    }
}